package com.changhai.diabetes.model.page;

import java.util.List;
import java.util.Map;

/**
 * @author Andy
 *
 * @param <T>
 */
public class Pagination<T> implements Page<T> {
	List<T> elements;
	protected int pageSize = 20;
	protected int pageNumber;
	protected int totalElements = 0;
	protected String parametes;

	public Pagination(int pageNumber, int pageSize, int totalElements, List<T> elements, Map parametesMap) {
		this.pageNumber = pageNumber;
		this.pageSize = pageSize;
		this.elements = elements;
		this.totalElements = totalElements;
		setParametes(parametesMap);
		recomputePageNumber();
	}

	public Pagination(int totalElements, List<T> elements, Map parametesMap) {
		this.elements = elements;
		this.totalElements = totalElements;
		setParametes(parametesMap);
		recomputePageNumber();
	}

	public Pagination(int pageNumber, int pageSize, Map parametesMap) {
		this.pageNumber = pageNumber;
		this.pageSize = pageSize;
		setParametes(parametesMap);
	}

	protected void recomputePageNumber() {
		if (Integer.MAX_VALUE == this.pageNumber || this.pageNumber > getLastPageNumber()) { // last
			// page
			this.pageNumber = getLastPageNumber();
		}
	}

	public boolean isFirstPage() {
		return getThisPageNumber() == 1;
	}

	public boolean isLastPage() {
		return getThisPageNumber() >= getLastPageNumber();
	}

	public boolean hasNextPage() {
		return getLastPageNumber() > getThisPageNumber();
	}

	public boolean hasPreviousPage() {
		return getThisPageNumber() > 1;
	}

	public int getLastPageNumber() {
		if (pageSize == 0)
			pageSize = 20;
		if (totalElements <= 0)
			return 1;
		return totalElements % this.pageSize == 0 ? totalElements / this.pageSize : totalElements / this.pageSize + 1;
	}

	public List<T> getThisPageElements() {
		return elements;
	}

	public int getTotalNumberOfElements() {
		return totalElements;
	}

	public int getThisPageFirstElementNumber() {
		return (getThisPageNumber() - 1) * getPageSize() + 1;
	}

	public int getThisPageLastElementNumber() {
		int fullPage = getThisPageFirstElementNumber() + getPageSize() - 1;
		return getTotalNumberOfElements() < fullPage ? getTotalNumberOfElements() : fullPage;
	}

	public int getNextPageNumber() {
		return getThisPageNumber() + 1;
	}

	public int getPreviousPageNumber() {
		return getThisPageNumber() - 1;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getThisPageNumber() {
		return pageNumber <= 0 ? 1 : pageNumber;
	}

	public void setParametes(String parametes) {
		this.parametes = parametes;
	}

	public void setParametes(Map m) {
		if (m == null) {
			this.parametes = "";
		}
		StringBuilder sb = new StringBuilder();
		for (Object o : m.keySet()) {
			if (sb.length() > 0) {
				sb.append("&");
			}
			sb.append(o.toString());
			sb.append("=");
			sb.append(m.get(o).toString());
		}
		this.parametes = sb.toString();
	}

	@Override
	public String getParametes() {
		// TODO Auto-generated method stub
		return "12345";
	}
}
